package com.ciwong.epaper.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.util.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/18.
 */

public class SubjectSelectDialog extends Dialog  implements  View.OnClickListener{


    private GridView subject_grid_view;

    private  SubjectAdapter mSubjectAdapter;

    private TextView btn_subject_all;

    private  OnSubjectSelectListener mOnSubjectSelectListener;

    public SubjectSelectDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
//        mRootView= LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_grade_select, null);
        setContentView(R.layout.dialog_select_subject);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtils.getScreenWidth(context.getApplicationContext()); //设置宽度
        getWindow().setAttributes(lp);
        btn_subject_all = (TextView) findViewById(R.id.btn_subject_all);

        findViewById(R.id.btn_subject_confirm).setOnClickListener(this);
        btn_subject_all.setOnClickListener(this);

        subject_grid_view = (GridView) findViewById(R.id.subject_grid_view);
        subject_grid_view.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mSubjectAdapter = new SubjectAdapter();

        List<String> subjectList = new ArrayList<>();
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");
        subjectList.add("英语");

        mSubjectAdapter.setSubjectList(subjectList);

        subject_grid_view.setAdapter(mSubjectAdapter);



        subject_grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                btn_subject_all.setTextColor(getContext().getResources().getColor(R.color.light_gray16));
                btn_subject_all.setBackgroundResource(R.drawable.btn_bg_gray_border4);

                mSubjectAdapter.setSelection(position);
                mSubjectAdapter.notifyDataSetChanged( );

            }
        });

    }


    @Override
    public void onClick(View v) {
        int id=v.getId();

        if (id == R.id.btn_subject_confirm) {

            if (null != mOnSubjectSelectListener) {

            }

            dismiss();

        } else if (id == R.id.btn_subject_all) {

            btn_subject_all.setTextColor(getContext().getResources().getColor(R.color.white));
            btn_subject_all.setBackgroundResource(R.drawable.selector_btn_bg_light_green_radius15dp);
            mSubjectAdapter.setSelection(-1);
            mSubjectAdapter.notifyDataSetChanged();
        }

    }


    public  interface  OnSubjectSelectListener{

        void onSubjectSelect(int subjectId,String subjectName);

    }




    class SubjectAdapter extends BaseAdapter {

        private List<String> filterTagList = new ArrayList<String>();
        private int clickSelectPos = -1;

        public SubjectAdapter() {

        }


        public void setSelection(int position) {
            clickSelectPos = position;
        }

        public void setSubjectList(List<String> list) {
            filterTagList = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return filterTagList.size();
        }

        @Override
        public Object getItem(int position) {
            return filterTagList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = inflater.inflate(R.layout.item_subject_name, null);
                holder = new ViewHolder();
                holder.tx_subject_name = (TextView) convertView.findViewById(R.id.tx_subject_name);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

//            holder.tx_subject_name.setText(filterTagList.get(position).getKindName());

            if (position == clickSelectPos) {
                holder.tx_subject_name.setTextColor(getContext().getResources().getColor(R.color.white));
                holder.tx_subject_name.setBackgroundResource(R.drawable.selector_btn_bg_light_green_radius15dp);
            } else {

                holder.tx_subject_name.setTextColor(getContext().getResources().getColor(R.color.light_gray16));
                holder.tx_subject_name.setBackgroundResource(R.drawable.btn_bg_gray_border4);
            }

            return convertView;

        }
    }



    class ViewHolder {
        public TextView tx_subject_name;
    }
}
